package org.lipert.rccalc.fragments;

import java.text.DecimalFormat;

import org.lipert.rccalc.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class MaxSpeedFragment extends SherlockFragment implements
		OnClickListener {
	private Button calculateMaxSpeedButton;
	private EditText kVMaxSpeedEditText;
	private EditText voltageMaxSpeedEditText;
	private EditText diameterMaxSpeedEditText;
	private EditText gearRatioMaxSpeedEditText;
	private TextView resultTextView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.max_speed_layout, container,
				false);

		calculateMaxSpeedButton = (Button) view
				.findViewById(R.id.calculateMaxSpeedButton);
		calculateMaxSpeedButton.setOnClickListener(this);
		
		resultTextView = (TextView)view.findViewById(R.id.resultMaxSpeedTextView);

		kVMaxSpeedEditText = (EditText) view
				.findViewById(R.id.kVMaxSpeedEditText);
		voltageMaxSpeedEditText = (EditText) view
				.findViewById(R.id.voltageMaxSpeedEditText);
		diameterMaxSpeedEditText = (EditText) view
				.findViewById(R.id.diameterMaxSpeedEditText);
		gearRatioMaxSpeedEditText = (EditText) view
				.findViewById(R.id.gearRatioMaxSpeedEditText);

		return view;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == calculateMaxSpeedButton.getId()) {
			float kV = Float
					.parseFloat(kVMaxSpeedEditText.getText().toString());
			float voltage = Float.parseFloat(voltageMaxSpeedEditText.getText()
					.toString());
			float diameter = Float.parseFloat(diameterMaxSpeedEditText
					.getText().toString());
			float gearRatio = Float.parseFloat(gearRatioMaxSpeedEditText
					.getText().toString());
			
			
			float maxRpm = kV * voltage;
			float wheelTurns = maxRpm / gearRatio;
			float circuit = (float) (Math.PI * diameter);
			float distancePerMinute = wheelTurns * circuit;
			float speedMS = distancePerMinute / 1000 / 60;
			float speedKH = (float) (speedMS * 3.6);
			
			DecimalFormat form = new DecimalFormat("0.00"); 
			resultTextView.setText(form.format(speedKH));
			
			
			
		}

	}
}
